package com.opennetexam.manager.mapper;

import com.opennetexam.manager.databasemodel.ExamPaper;
import com.opennetexam.manager.databasemodel.ExamPaperItem;
import com.opennetexam.manager.sqlprovider.ExamPaperProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author naicha
 * Created on 2017/11/15.
 */
@Mapper
public interface ExamPaperMapper {

    @SelectProvider(type = ExamPaperProvider.class,method = "getExamPapersBySomeWhere")
    List<ExamPaper> getExamPapersBySomeWhere(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") String examPlanInfoID, @Param(value = "examSubjectID") String examSubjectID);

    @Select("SELECT * FROM ExamPaperItem WHERE PaperID = #{paperId}")
    List<ExamPaperItem> findExamPaperItemsByPaperId(@Param(value = "paperId") String paperId);

    @Select("SELECT * FROM ExamPaper WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} AND ExamSceneID = #{examSceneID} AND ExamSubjectID = #{examSubjectID} AND PaperID = #{paperID}")
    List<ExamPaper> findExamPaperByAppIDAndPlanId(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") String examPlanInfoID, @Param(value = "examSceneID") String examSceneID, @Param(value = "examSubjectID") String examSubjectID, @Param(value = "paperID") String paperID);

    @Insert("INSERT INTO ExamPaper (\n" +
            "\tExamPaperID,AppID,ExamPlanInfoID,ExamSubjectID,PaperID,PaperVersion,ExamPaperContent\n" +
            ") SELECT #{examPaperID},#{appID},#{examPlanInfoID},#{examSubjectID},#{paperID},#{paperVersion},#{examPaperContent}\n" +
            "FROM\n" +
            "\tDUAL\n" +
            "WHERE\n" +
            "\tNOT EXISTS (\n" +
            "\t\tSELECT\n" +
            "\t\t\t1\n" +
            "\t\tFROM\n" +
            "\t\t\tExamPaper\n" +
            "\t\tWHERE\n" +
            "\t\t\tExamPlanInfoID = #{examPlanInfoID}\n" +
            "\t\tAND ExamSceneID = #{examSceneID}\n" +
            "\t\tAND ExamSubjectID = #{examSubjectID}\n" +
            "\t\tAND PaperID = #{paperID}\n" +
            "\t\tLIMIT 1\n" +
            ")")
    Integer insertExamPaperIFNotExist(ExamPaper examPaper);

    @Insert("INSERT INTO ExamPaper (ExamPaperID,AppID,ExamPlanInfoID,ExamSubjectID,PaperID,PaperVersion,ExamPaperContent) VALUE (#{examPaperID},#{appID},#{examPlanInfoID},#{examSubjectID},#{paperID},#{paperVersion},#{examPaperContent})")
    Integer insertExamPaper(ExamPaper examPaper);

    @Update("UPDATE ExamPaper SET AppID = #{appID},ExamPlanInfoID = #{examPlanInfoID},ExamSceneID = #{examSceneID},ExamSubjectID = #{examSubjectID},PaperID = #{paperID},PaperVersion = #{paperVersion},ExamPaperContent = #{examPaperContent} WHERE ExamPaperID = #{examPaperID}")
    Integer updateExamPaper(ExamPaper examPaper);

    @Select("SELECT * FROM ExamPaper WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID}")
    List<ExamPaper> findExamPaperListByAppIDAndPlanId(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") Integer examPlanInfoID);

    @Select("SELECT * FROM ExamPaper WHERE ExamPlanInfoID = #{planId}")
    List<ExamPaper> findExamPaperByPlanId(@Param("planId") String planId);

    @Delete("DELETE FROM ExamPaper WHERE ExamPlanInfoID = #{planId}")
    void delExamPaperByPlanId(@Param("planId") String planId);

    @Select("SELECT ExamPaperID,ExamPaperContent FROM ExamPaper WHERE ExamPlanInfoID = #{examPlanID} AND ExamSubjectID = #{examSubjectID}")
    List<ExamPaper> finaAllExamPaper(@Param(value = "examPlanID") String examPlanID, @Param(value = "examSubjectID") String examSubjectID);

    @Select("SELECT ExamPaperID FROM ExamPaper WHERE ExamPlanInfoID = #{examPlanID} AND ExamSubjectID = #{examSubjectID} AND PaperID = #{paperID} LIMIT 1")
    List<ExamPaper> finaExamPaperBySomeWhere(@Param(value = "examPlanID") String examPlanID, @Param(value = "examSubjectID") String examSubjectID, @Param(value = "paperID") String paperID);

    @Select("SELECT ExamPaperContentJson FROM ExamPaper WHERE ExamPaperID = #{examPaperID}")
    String getExamPaperByID(@Param(value = "examPaperID") String examPaperID);
}
